package cn.kgc.ssm.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.ssm.service.SlideService;
import cn.kgc.ssm.utils.ResponseResult;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2024/3/7
 * @description   轮播图控制层
 */
@Controller
@RequestMapping("slide")
public class SlideShowController {

    @Resource
    private SlideService slideService;

    // 轮播图分页数据的加载
    @GetMapping("page")
    @ResponseBody
    public ResponseResult  slideShowPage(Integer page,Integer limit){

        return  slideService.slideShowPage(page,limit);
    }


    //  根据图片的名字 获取图片  getSlidePicture    uuid 图片名字
    @GetMapping("getSlidePicture")
    public void  getSlidePicture(String uuid, HttpServletResponse response) throws Exception {

        // 通过io流 读取本地服务器中的图片
        File uuidImg = new File("D:/upload/slides",uuid);
        FileInputStream fileInputStream = new FileInputStream(uuidImg);
        ServletOutputStream outputStream = response.getOutputStream();

        // 将读取到的图片以流的形式响应给前端
        IOUtils.copy(fileInputStream,outputStream);

        // 释放资源
        outputStream.close();
        fileInputStream.close();

    }

    //图片上传接口  post
    @PostMapping("upload")
    @ResponseBody
    public ResponseResult  uploadPicture(@Param("file") MultipartFile file) throws IOException {

        // 1.获取文件的名字
        String originalFilename = file.getOriginalFilename();  // lb1.png
        // 2. 获取文件的扩展名
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);  //  png  jpg
        // 3. 生成uuid
        String uuid = IdUtil.simpleUUID();
        String uuidName = uuid+"."+filenameExtension;

        // 4. 将图片存储在本地服务器
        File slideDirectory = new File("D:/upload/slides");
        if (!slideDirectory.exists()){
            slideDirectory.mkdir();
        }

        File file1 = new File(slideDirectory, uuidName);
        // 将文件写入本地磁盘
        file.transferTo(file1);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setData(uuidName);
        return responseResult;
    }




    @PostMapping("uploadInfo")
    @ResponseBody
    public ResponseResult  uploadInfo(String title,String uuid){

        return slideService.addUploadInfo(title,uuid);
    }


    // 置顶轮播图
    @GetMapping("edit")
    @ResponseBody
    public ResponseResult  editSlide(Integer id){

        return slideService.editSlide(id);
    }



    // 删除轮播图
    @GetMapping("delete")
    @ResponseBody
    public ResponseResult deleteSlide(String uuid){

        return slideService.deleteSlide(uuid);
    }


    // 前端轮播图列表
    @GetMapping("list")
    @ResponseBody
    public ResponseResult slideList(){
        return slideService.slideList();
    }
}
